<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="className">
        <el-input v-model="queryParams.className" placeholder="请输入分类名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="success" size="small" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['decor:classType:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-if="refreshTable" v-loading="loading" :data="classTypeList" row-key="classId"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column label="分类名称" align="center" prop="className">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.className"/> -->
        </template>
      </el-table-column>
      <el-table-column label="父产品id" align="center" prop="parentId">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.parentId"/> -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="祖级列表" align="center" prop="ancestors">
        <template #default="scope">
          <dict-tag :options="${column.dictType}" :value="scope.row.ancestors"/>
        </template>
      </el-table-column> -->
      <el-table-column label="产品状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_common_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.orderNum"/> -->
        </template>
      </el-table-column>
      <el-table-column label="处理类型" align="center" prop="handlerType">
        <template #default="scope">
          <dict-tag :options="handler_type" :value="scope.row.handlerType" />
        </template>
      </el-table-column>
      <el-table-column label="处理凭证" align="center" prop="condition">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.condition"/> -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['decor:classType:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)"
            v-hasPermi="['decor:classType:add']">新增</el-button>
          <el-button link type="warning" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['decor:classType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设计小程序的分类标签对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="classTypeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类" prop="parentId">
          <el-tree-select v-model="form.parentId" :data="classTypeOptions"
            :props="{ value: 'classId', label: 'className', children: 'children' }" value-key="classId"
            placeholder="请选择分类" check-strictly />
        </el-form-item>
        <el-form-item label="分类名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_student_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <!-- <el-form-item label="处理类型" prop="handlerType">
          <el-radio-group v-model="form.handlerType">
            <el-radio v-for="dict in handler_type" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理凭证" prop="condition">
          <el-input v-model="form.condition" placeholder="请输入处理凭证" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listClassType, getClassType, delClassType, addClassType, updateClassType } from "@/api/decor/classType";

const { proxy } = getCurrentInstance();
const { sys_student_status } = proxy.useDict('sys_student_status');
const { handler_type } = proxy.useDict('handler_type');
const { sys_common_status } = proxy.useDict('sys_common_status')

// 树表表格数据
const classTypeList = ref([]);
// 树选项
const classTypeOptions = ref([]);
// 是否显示弹出层
const open = ref(false);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 弹出层标题
const title = ref("");
// 是否展开，默认全部展开
const isExpandAll = ref(true);
// 重新渲染表格状态
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    className: null,
    parentId: null,
    ancestors: null,
    status: null,
    orderNum: null,
  },
  rules: {
    className: [
      { required: true, message: "分类名称不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父产品id不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "产品状态不能为空", trigger: "blur" }
    ],
    orderNum: [
      { required: true, message: "排序不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设计小程序的分类标签列表 */
function getList() {
  loading.value = true;
  listClassType(queryParams.value).then(response => {
    classTypeList.value = proxy.handleTree(response.data, "classId", "parentId");
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    classId: null,
    className: null,
    tenantId: null,
    parentId: null,
    ancestors: null,
    status: null,
    orderNum: null,
    version: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("classTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
async function handleAdd(row) {
  reset();
  await listClassType().then(response => {
    classTypeOptions.value = proxy.handleTree(response.data, "classId");
  });
  if (row != null && row.classId) {
    form.value.parentId = row.classId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加分类";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await listClassType().then(response => {
    classTypeOptions.value = proxy.handleTree(response.data, "classId");
  });
  if (row != null) {
    form.value.parentId = row.classId;
  }
  getClassType(row.classId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改分类";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["classTypeRef"].validate(valid => {
    if (valid) {
      if (form.value.classId != null) {
        updateClassType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClassType(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    } else {
      proxy.$modal.msgError('请根据提示完善必填信息')
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除设计小程序的分类标签编号为"' + row.classId + '"的数据项？').then(function () {
    return delClassType(row.classId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('decor/classType/export', {
    ...queryParams.value
  }, `classType_${new Date().getTime()}.xlsx`)
}

getList();
</script>
